WP How to Create Link for Custom Template? - php

I cannot get working my code and I thought that best way is asking here.
so as on the title
Im looking to create a function (link for a specific template file) for my template-contact.php
Tried to do with this function wich I found in another topic but it didnt work.
function get_contact_page() {
$contact_page = get_pages(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template-contact.php'
)
);
$contact_id = $showcase_page[0]->ID;
echo get_permalink( $contact_id );
}
but when I use
<a href="<?php echo get_contact_page(); ?>" class="widget" data-toggle="tooltip" data-placement="bottom" data-title="CONTATTO"><span
class="ico ico-phone-btn"></span></a>
does nothing.
Thanks for all your help

get_pages() has an issue with meta_key and meta_value parameters. I recommend using WP_Query instead:
function get_contact_page() {
$query = new WP_Query(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template-contact.php',
'fields' => 'ids',
)
);
return $query->have_posts() ? get_permalink( $query->posts[0] ) : '#';
}

Try
echo get_permalink( $archive_id );
change to
return get_permalink( $archive_id );

Related

Change posts_per_page Into MySQL Query for WooCommerce AJAX Live Search

Using posts_per_page might not be the best option and I'm wondering if it should be changed into a MySQL query to be quicker and add less stress to site / server?
This is a process for building a live product search for WooCommerce.
This is the code:
add_action( 'wp_ajax_data_fetch' , 'data_fetch' );
add_action( 'wp_ajax_nopriv_data_fetch', 'data_fetch' );
function data_fetch() {
$post_search_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['search_keyword'] ), 'post_type' => 'product' ) );
if( $post_search_query->have_posts() ) :
while( $post_search_query->have_posts() ): $post_search_query->the_post(); ?>
<h5><?php the_title();?></h5>
<span class="live-search-post-excerpt"><?php the_excerpt(); ?></span>
<?php endwhile;
wp_reset_postdata();
endif;
die();
}
Whereof I'm wondering if there's a way to change this:
$post_search_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['search_keyword'] ), 'post_type' => 'product' ) );
into an SQL query and if so - how?
You can use wpdb.
global $wpdb;
$data = $wpdb->get_results("SELECT * FROM table");

retrieving and display images by tag in wordpress

i iam creating a section with tags with this code, is a function to retrieve tags and exclude some tags also,
$args = array('name__like' => $name_like, 'exclude' => array(75,177,42,74,197,36,40,140,162,108,86,47,4,29,22,215,87,151,104),'order' => 'ASC');
$tags = get_tags( $args );
if ( !empty( $tags ) && !is_wp_error( $tags ) ) {
$count = count($tags);
$i=0;?>
<ul class="my_term-archive">
<?php
foreach ($tags as $tag) {
$i++;
$tag_link = get_tag_link( $tag->term_id );
$tag_id = get_tag_ID($tag->name);
if(strtolower(substr($tag->name,0,1)) !=$name_like){
continue;
}
//i need a function here to retrieve images with the id of the tag
//attached
//////
$html .= "<li><a href='{$tag_link}' id='{$tag_id}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}
}
echo $html;
?>
</ul>
then i put this code in my functions.php file in wordpress, to make avaliable the tag box in the picture managment, so i can tag pictures now,
function wptp_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
so my question is how can find and display the images by the id tag ?
sorry my bad english, is not my native lenguage. any help is very welcome. thanks
You can actually handle this with a basic WP_Query call. There's lots of details and options for the WP_Query object here, but I'd think you could do something like this:
$args = array(
'post_type' => 'attachment',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'whatever-your-tag-slug-is',
),
),
);
$query = new WP_Query( $args );

How to write more efficient fallback for empty wp_query in Wordpress

I have a WooCommerce store where I want to display a featured image & heading of one of the following (in order):
Featured Product
If no featured product, then sticky post
If no sticky post, then most recent post
But I also want to write efficient code. How do I simplify this and remove redundant PHP and HTML?
/* START FEATURED PRODUCT QUERY */
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_query' => array(
'key' => '_featured',
'value' => 'yes'
),
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
} else {
/* START FALLBACK POST QUERY */
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
}
The second WP_Query has the exact same HTML output, just different $args
I’m writing a similar query and I’m not sure you can make the query much more efficient in Wordpress than you already have. The only thing I did differently was to make the output of the posts a function so that it calls the same code. This’ll make it easier to update.
Also since you’re only querying one meta field in the first query I switched to a simple custom field query.
// Function to output posts
function output_posts( $query ){
while( $query->have_posts() ) {
$query->the_post();
echo '<a href="' . get_permalink() '" id="featured-blog-post">';
the_post_thumbnail( 'full' );
the_title( '<h2>', '<span>»</span></h2>' );
the_excerpt();
echo '</a>';
}
wp_reset_postdata();
}
// Featured query
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
);
$featured = new WP_Query( $args );
// If featured has posts
if( $featured->have_posts() ) {
// Output
output_posts( $featured );
// Else fallback
} else {
// Fallback query
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1,
);
$fallback = new WP_Query( $args );
// If fallback has posts
if ( $fallback->have_posts() ){
// Output
output_posts( $fallback );
}
}

Content loaded from different queries mixed up / wrong content displayed

I have a very strange bug on my wordpress site:
in the header.php i'm loading images like that:
<?php
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'tax_query' => array(
array(
'taxonomy' => 'media_category',
'field' => 'name',
'terms' => 'redimages'
)
)
);
$the_query2 = new WP_Query( $args );
if ( $the_query2->have_posts() ) {
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
$postid = get_the_ID();
$image_attributes = wp_get_attachment_image_src( $postid , 'full');
if ( $image_attributes ) : ?>
<img src="<?php echo $image_attributes[0]; ?>" class="backgroundimage"/>
<?php endif;
}
}
wp_reset_postdata();
?>
and inside functions.php i have a custom function to load the content of a page:
function pf_show_page($id) {
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
now my problem is, that one image from the first query is always displayed, when i call the pf_show_page function, although there are no images in the pages I load.
The strangest thing is that this only happens when the 404.php is opened, but the images show up in the header.php region.
I copied the queries from the other templates to the 404 document and it is still happening.
Maybe there's something wrong outside my template files?
Tried my best to describe my problem. Thank you for reading

Wordpress- Get Posts by Custom Field

I'm trying to make a Wordpress plugin for my blog that scans for posts that contain the custom field _videoembed. I've got everything made and it activates correctly but I receive a PHP error when I open up a page to test it out:
Fatal error: Call to a member function get_results() on a non-object in .../wp-content/plugins/youtubesubscription/videos.php on line 26.
Does anyone know enough about PHP to help me out? Here's my plugin code (pasted on pastebin because of size):
http://pastebin.com/uaEWjTn2
EDIT 1
I'm no longer getting any errors after inserting global $wpdb; but now nothing's showing up. Here's my updated code: http://pastebin.com/R2ZuEknY. Note, this code also incorporates auto YouTube thumbnails, which were obtained from a function that trims YouTube URLs to the IDs (link).
EDIT 2
Got it working, turns out all I needed to do was insert '_videoembed' as the 'meta_key' argument for a wp_query. Here's my working code below:
<?php
$args = array(
'meta_key' => '_videoembed',
'post_status' => 'publish',
'posts_per_page' => '' . $number . '',
'order' => 'date'
);
query_posts( $args );
while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$VIDEOID = ytvideoID($post->ID);
?>
<li onClick="window.location.href='<?php the_permalink(); ?>'">
<?php
global $post;
$videoId = ytvideoID($post->ID);
$videoInfo = parseVideoEntry($videoId);
echo '<a href="'.get_permalink().'">';
echo '<div class="image">';
echo '<img src="http://img.youtube.com/vi/'.$VIDEOID.'/default.jpg">';
echo '<div id="time" style="position:absolute;z-index:9;bottom:2px;right:2px;font-size:10px;color:#fff;background:#000;padding:0px 2px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;opacity:0.75;">'.hms($videoInfo->length).'</div>';
echo '</div>';
echo '<h4>'.get_the_title().'</h4>';
echo '<div id="description">';
echo '<div id"views"><h3>'.number_format($videoInfo->viewCount).' Views</h3></div>';
echo '<div class="singleauthor"><h3>by '.$videoInfo->author.'</h3></div>';
echo '</div>';
echo '</a>';
?>
</li>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
you get the post by custom field using the meta_query.
$args= array(
'category_name' => 'courses',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'front_page',
'value' => 'yes',
'compare' => 'LIKE',
))
);
$the_query = new WP_Query( $args );
please refer my tutorial for more details.
http://www.pearlbells.co.uk/filter-posts-custom-fields-wp_query/
You'll have to to add global $wpdb;
try replacing
function mbrecentvids()
{ ?>
<?php
with
function mbrecentvids()
{
global $wpdb;
It looks like you're missing a semi-colon on line 26.
I see:
$pageposts = $wpdb->get_results($querydetails, OBJECT_K)

Categories