How to add permalink into WP_Query in Wordpress Theme - php

I want to add Blog Page in front-page.php.I want to show only post title with link to actual post.I dont know where to put the_permalink in my code.My code like below:
<div class="profile">
<?php
$query = new WP_query( 'pagename=blog' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ){
$query->the_post();
echo '<h2 class="text-center">' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
?>
</div><!--end blog-content -->
When I put ,the hyperlink goes to domain.com/get_permalink() not to actual url.
Example the actual url is domain.com/abc.
When I click to the post title,it goes to domain.com/get_permalink()
Anyone can help me to solve my problem?

May be you are using get_permalink in wrong way. Try following code.
<div class="profile">
<?php
$query = new WP_query( 'pagename=blog' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ){
$query->the_post();
echo '<h2 class="text-center"><a href="' . get_permalink() . '">';
the_title();
echo '</a></h2>';
}
}
wp_reset_postdata();
?>
</div><!--end blog-content -->

Related

Display ACF field inside posts loop in category.php

OK, so I need to display ACF custom field inside my posts loop in my custom category.php file. Here is the loop:
<div class="container">
<div class="row">
<?php
if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>
<div class="col-xs-12 col-sm-4">
<?php the_title( '<h2>', '</h2>' ); ?>
<div><?php MY_ACF_FIELD_GOES_HERE ?></div>
</div>
<?php
/* End the Loop */
endwhile;
?>
</div><!-- .row -->
</div><!-- .container -->
As you can see loop displays pages from category (titles), but I need also display short description. I know I could use:
<?php the_excerpt(); ?>
But not in this case because excerpt contains text that I do not need inside loop. So I need to create my own short description field for every page. How can I display it in category.php template? Custom field (my own short desc) is on all pages.
You can retrieve the ACF field value using get_field('field_name'). Example-
<?php
$args = array( 'post_type' => 'speakers', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="entry-content">';
echo '<h2 class="speaker-name">';
the_title();
echo '</h2>';
echo '<img src="' . get_field('field_name') . '" alt="" />';
echo '<span class="speaker-title">';
the_field('title'); echo ' / '; the_field('company_name');
echo '</p>';
the_content();
echo '</div>';
endwhile;
?>

WordPress: get_permalink() doesn't take me from the excerpt to the full post

If one day someone else is looking at this in the future with the same problem, here is an update:
UPDATE: Okay, you need to create a single.php and put the loop in that with the_content();
I have a problem here and hopefully it is something small that I over looked.
What I want:
I want only an excerpt of a post to be displayed with a link underneath in WordPress. When I click on the link I want it to show me the full post.
What I tried:
I looked at the WordPress Codex to find out how to do this and read to put this code into my file:
Read More...
or
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) .
'">' . __('Read More', 'your-text-domain') . '</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
What happens:
A link will appear under the excerpt however when I click on it I am taken to a page with just an excerpt rather than the full post.
My request:
What change would I have to make to have the permalink take me to the full post? Below is a list of my code from content.php
<?php
if (have_posts()) :
while ( have_posts() ) : the_post();
?>
<p style="color:white"><?php the_title(); ?></p>
<div class="postContent" style="align:center;">
<?php the_excerpt(); ?>
Read More...
</div>
<?php
endwhile;
else :
?>
<p>Sorry no posts matched your criteria.</p>
<?php
endif;?>
get_permalink() always send you to full post detail page. -> in your case may be you have edited page.php, single.php or function.php to display content limit. -> or check your theme options area for the content limit option if you are using any purchased theme.
I have facing the same issue and I used the below code and it works prefectly fine for me.
function custom_excerpt_more( $more ) {
return sprintf( '<br /><a class="read-more" href="%1$s">%2$s</a>',
get_permalink( get_the_ID() ),
__( 'Read More >>', 'textdomain' )
);
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
You can try this edited code
<?php
if (have_posts()) :
while ( have_posts() ) : the_post();
?>
<p style="color:white"><?php the_title(); ?></p>
<div class="postContent" style="align:center;">
<?php the_excerpt(); ?>
Read More...
</div>
<?php
endwhile;
else :
?>
<p>Sorry no posts matched your criteria.</p>
<?php
endif;?>

How to use fancyBox to show larger view of post thumbnail (in the WP Loop)

Has anyone been able to use fancyBox to open the post thumbnail in the WordPress loop? Here is my code so far:
<?php
$query = new WP_query( 'pagename=about');
//The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<aside class="clear flex-container">';
echo '<div class="about">';
echo '<figure class="about-thumb">';
echo '<a href="#" class="fancybox-thumb" rel="fancybox- thumb">';
the_post_thumbnail('medium');
echo '</a>';
echo '</figure>';
echo '<div class="about-entry-content">';
the_content();
echo '</div>';
echo '</div>';
echo '</aside>';
}
}
/* Restore original Post Data */
wp_reset_postdata();
?>
I have not been able to find an answer to this. Any help appreciated. Thanks.

Wordpress loops efficiency

I build this site with wordpress:
http://mida.org.il/
As you can see, homepage takes a lot of time to load.
I'm trying to fix this - there are five custom loops in that page, three of them using posts_per_page and cat to query posts, and posts_per_page set to 3.
My question is, if the loop gets to the third post, it stops and and breaks out, or its keep looping until it gets to the last post?
If the second is correct, no wonder that it's so slow, this site holds thousands of posts.
The code for the loops:
if ( $first_special_cat ){
$args = array( 'cat'=>$first_special_cat, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky );
$cat_name = $first_special_cat;
$cat_id = get_cat_ID($first_special_cat);
}else{
$args = array( 'cat'=>50, 'posts_per_page'=>3, 'orderby'=>'date', 'post__not_in'=>$sticky );
$cat_name = get_cat_name(50);
$cat_id = 50;
}
$the_query = new WP_Query($args);
echo '<div class="special-proj-main-title">';
echo '<div class="homepage-blueline-title"></div>';
echo '<h4 class="special-cat-name"><a href="' . esc_url( get_term_link($cat_id) ) . '">' . $cat_name . '</h4>';
echo '</div>';
?>
<div class="row">
<div class="col-sm-4">
<?php if ( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); //Setting the three posts to the right: ?>
<h2 class="special-project-title"><a class="special-proj-title-link" href="<?php echo esc_url( get_the_permalink() )?>"><?php the_title()?></a></h2> <br/>
<div class="post-meta special-project-meta"><?php mida_post_meta()?></div><br/>
<?php
endwhile;
wp_reset_postdata(); ?>
<span class="to-all-posts"><?php echo sprintf( __('Load more posts from %s', 'mida'), $cat_name ); ?></span>
<?php
else:
echo "You put wrong id";
endif;
?>
</div>
<div class="col-sm-8 home-background-img">
<?php
if ( $first_special_post )
$args = array('name' => $first_special_post, 'posts_per_page' => 1 );
else
$args = array('cat'=>50, 'posts_per_page' => 1, 'orderby'=>'date' );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$first_special_img = get_field('rectangular_image');
if ( $first_special_img )
$first_special_img_src = wp_get_attachment_image_src( $first_special_img['id'], 'full' );
else
$first_special_img_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
?>
<div class="special-project-section" style="background: url('<?php echo $first_special_img_src[0]; ?>');background-size: contain;">
<a href="<?php echo esc_url( get_the_permalink() )?>" title="<?php the_title() ?>"><span style="
position:absolute;
width:100%;
height:100%;
top:0;
left:0;
z-index: 1;"></</span>
</a>
<?php
echo '<div class="special-cat-on-img">';
echo $first_special_text ? '<h5><div class="special-cat-name-img">' . $first_special_text . '</div></h5>' : '<h5><div class="special-cat-name-img">' . __('Special Project', 'mida') . '</div></h5>'; ?>
<h6 class="speical-cat-title-img"> <?php the_title() ?> </h6>
<?php echo '</div>'; ?>
<div class="blue-line"><?php echo '<div class="special-proj-ex">' . $first_special_cat_ex . '</div>'; ?></div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div>
</div>
X3.
every loop query different categories ( $first_special_ are custom fields, input from the user ).
So can anyone help me optimize this code (and answer the above question)?
Thanks!
You problem is NOT in your loop but rather in the page itself. Total page size is a whopping 11.8MB! This looks primarily due to a ton of images. You might try a little image optimization (use jpg's for post thumbs/images) and make sure images are sized correctly. Honestly, lazy loading might be a good solution here! There are plenty of options out there to make it easy to implement.

get_the_date() not repeating date

<div class="page-content">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php the_title(); ?>
<br />
<?php
the_content();
echo get_the_date('l,d');
endwhile;
endif;
?>
</div>
This example is not repeating the date for each post. I am using the code in a page template, namely services-template.php and I have read the note that states get_the_date() will repeat the date for each post. Can someone help me figure out what I am doing wrong?
I could not make the original code work, I do not know all the internal workings of Wordpress but my guess is that what ever variable holds post data (most likely $post) was empty. The following code did what I was looking to do. Again I am using this code on a custom page. services-template.php.
$args = array( 'category' => '4' );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li><h2>' . get_the_title() . '</h2></li>';
echo '<li>' . get_the_date() . '</li>';
echo '<li>' . get_the_excerpt() . '</li>';
the_tags();
echo '<br /><br />';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
;
?>

Categories