简体   繁体   English

如何制作按类别插入的图像,链接到父帖子页面而不是其所在的类别页面。Wordpress

[英]How to make, an image that is pulled in by category, link to a parent posts page not to its category page it is located in. Wordpress

I'm new to php (' still struggle with syntax :-) ') and wordpress and I'm building a web-site where I am looping to my category page all the images tagged with that category. 我是php新手('仍然在语法上苦苦挣扎:-)')和wordpress,我正在建立一个网站,在该网站中,我将所有标记有该类别的图像循环到我的类别页面。 I need the images to be automatically linked to its parent post which should be a Clients page essentially. 我需要将图像自动链接到其父帖子,该帖子本质上应该是“客户”页面。 The code i'm using right now links me to the same category page instead of client page. 我现在使用的代码将我链接到同一类别页面,而不是客户端页面。

This is the code i'm using now to pull the images. 这是我现在用来提取图像的代码。

<?php 
    $query_images_args = array(
        'cat' => 3,
        'post_type'      => 'attachment',
        'post_mime_type' => 'image,video',//img & video files include
        'post_status'    => 'inherit',
        'orderby'        => 'ACS',
        'posts_per_page' =>  30,
    );

    $query_images = new WP_Query( $query_images_args );

    if($query_images->have_posts()) : 
        while($query_images->have_posts()) : 
            $query_images->the_post();
?>


<a href="<?php get_permalink( $parent_id ); ?>" rel="bookmark" title="<?php the_title(); ?>">

        <?php echo $images = wp_get_attachment_image( $query_images->posts->ID, 'thumbnail' ); ?>

</a>  

        <?php endwhile; ?>
    <?php else : ?>
        <p>No media file yet</p>
    <?php endif;

    /* Restore original Post Data */
    wp_reset_postdata(); ?>      

I hoped that this part <a href="<?php get_permalink( $parent_id ); ?> will link me to the clients page but it still links me to category page. 我希望这部分<a href="<?php get_permalink( $parent_id ); ?>将我链接到客户页面,但仍将我链接到类别页面。

I think that the I have something wrong with hierarchy or the way do the linking part. 我认为层次结构或链接部分的方式有问题。

I have also checked this resource but it does not seem to do anything : https://wordpress.stackexchange.com/questions/188736/get-the-title-and-url-of-the-attachment-parent-post 我也检查了此资源,但它似乎没有做任何事情: https : //wordpress.stackexchange.com/questions/188736/get-the-title-and-url-of-the-attachment-parent-post

I think that you are using a variable $parent_id that isn't set. 我认为您使用的是未设置的变量$ parent_id。

Try replacing: 尝试更换:

<a href="<?php get_permalink( $parent_id ); ?>"

with: 有:

<a href="<?php get_permalink( $post->post_parent ); ?>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM