Facebook Share Link Not Correct - php

I have a WordPress site that when you click the Facebook like button it likes the wrong url.
If I inspect the web page, the url that is inside the like widget is always the same URL no matter what post you are on. It is like one of the posts url is being set somewhere.
I have checked and it ONLY does it for posts, not pages.
I feel like I have found the area (might be wrong) but I believe it is here in the loop
<?php if (have_posts()) :
$post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h3><?php printf(__('Archive for the ā€˜%sā€™ Category', TEXTDOMAIN), single_cat_title('', false)); ?></h3>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h3><?php printf(__('Posts Tagged ā€˜%sā€™', TEXTDOMAIN), single_tag_title('', false) ); ?></h3>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h3><?php printf(__('Archive for %s | Daily archive page', TEXTDOMAIN), get_the_time(__('F jS, Y', TEXTDOMAIN))); ?></h3>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h3><?php printf(__('Archive for %s | Monthly archive page', TEXTDOMAIN), get_the_time(__('F Y', TEXTDOMAIN))); ?></h3>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h3><?php printf(__('Archive for %s | Yearly archive page', TEXTDOMAIN), get_the_time(__('Y', TEXTDOMAIN))); ?></h3>
<?php /* If this is a yearly archive */ } elseif (is_search()) { ?>
<h3><?php printf( __( 'Search Results for: %s', TEXTDOMAIN ), '<span>' . get_search_query() . '</span>' ); ?></h3>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h3><?php _e('Author Archive', TEXTDOMAIN); ?></h3>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<h3><?php _e('Blog Archives', TEXTDOMAIN); ?></h3>
<?php }
while (have_posts()) : the_post();
global $more;
if(!is_single()) $more = 0;
$type_blog = ( $GLOBALS['yiw_custom_blog_type'] != false ) ? $GLOBALS['yiw_custom_blog_type'] : get_option( $GLOBALS['shortname'] . '_blog_type', 'classic' );
?>
It is setting the $post but I am not sure how to fix it or check that this is in fact it. If I remove it, the page does not work.
Any help here will help a lot, if I am not correct what else should I be looking for?
The following are also areas in the functions.php that have the $post in it
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = get_stylesheet_directory_uri()."/images/default.gif";
}
return $first_img;
}
and
function get_current_ID()
{
global $post;
return $post->ID;
}
function get_current_pagename()
{
global $post;
return $post->post_name;
}

Related

"Exclude current post" Widget wordpress (PHP help)

Hi :) I am working on my site with the "flexible post widget" to show the related contents or the content that I am interested in showing on the sidebar.
The widget works very well and I'm interested in continuing to use it, especially for the design and adaptability options with my site.
The problem? It does not include a function for "exclude the current post".
I was reviewing the code (I add it to this thread) and I think I could add some lines of code for this function (exclude the post I'm reading).
Unfortunately I'm new to programming and I'm lost with what and where to add it.
Could you help me?
<?php
/**
* Flexible Posts Widget: Default widget template
*
* #since 3.4.0
*
* This template was added to overcome some often-requested changes
* to the old default template (widget.php).
*/
// Block direct requests
if ( !defined('ABSPATH') )
die('-1');
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
if ( $flexible_posts->have_posts() ):
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'widget_posts_args', 'be_exclude_current_post' );
?>
<ul class="dpe-flexible-posts">
<?php while ( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post; ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php echo the_permalink(); ?>">
<?php
if ( $thumbnail == true ) {
// If the post has a feature image, show it
if ( has_post_thumbnail() ) {
the_post_thumbnail( $thumbsize );
// Else if the post has a mime type that starts with "image/" then show the image directly.
} elseif ( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
echo wp_get_attachment_image( $post->ID, $thumbsize );
}
}
?>
<div class="title"><?php the_title(); ?></div>
</a>
</li>
<hr>
<?php endwhile; ?>
</ul><!-- .dpe-flexible-posts -->
<?php
endif; // End have_posts()
echo $after_widget;
Add this to your theme's 'functions.php' and remove it from the widget template.
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'dpe_fpw_args', 'be_exclude_current_post' );

Wordpress - targeting images in content

I have the following code in my wordpress theme, single-post page (the page where single posts are displayed).
<article id="post-<?php the_ID(); ?>"<?php post_class('col-md-12'); ?>>
<div class="container">
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<?php
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
$image = aq_resize( $img_url, 1200, 720, true ); //resize & crop the image
?>
<?php if($image) : ?>
<img class="img-responsive singlepic" src="<?php echo $image ?>"/>
<?php endif; ?>
<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
}
}
}
?>
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'web2feel' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</div>
</article><!-- #post-## -->
It puts the post-title then featured-image as a large image then posts the content, and then the rest.
I want to target the images in the content, and display them the same way as the featured-image is displayed.
*NOTE - changes to the 'if ( $attachments ) { ...' function doesn't change anything (nor does completely removing it) so I am not certain what that part does. the only code that affects the content is when it is called ( '')
Sometimes ago I used to following code to strip off all the images in my content area and then display them separately apart from the contents. This might help you.
In your functions.php file -
function stripe_images_from_content($content){
$images = array();
$subject = $content;
$subject = preg_replace('~<img [^\>]*\ />~', '', $subject);
$subject = preg_replace('~\[caption[^\]]*\][^\]]*\]~', '', $subject);
$image_urls = array();
$match_count = preg_match_all( '/<img.*src=["\']([^"\']*)["\'].*\/>/', $content, $image_urls );
if (is_numeric($match_count) && $match_count > 0) {
if( is_array($image_urls[1]) and count($image_urls[1]) > 0 ){
foreach( $image_urls[1] as $i ){
$featured_image_url = $i;
$image_id = get_attachment_id_from_src( $featured_image_url );
if (! is_numeric($image_id)) {
$match_count = preg_match( '/(.*)-\d+x\d+(\.(jpg|png|gif))/', $featured_image_url, $matches );
if (is_numeric($match_count) && $match_count > 0) {
$base_image_url = $matches[1] . $matches[2];
$images[] = get_attachment_id_from_src( $base_image_url );
}
}
else{
$images[] = $image_id;
}
}
}
}
$data = new stdClass();
$data->images = $images;
$data->content = $subject;
return $data;
}
What this function does is get all the images inserted in the content area of a post and strip them off. Then return the image(s) and the content without image.
in your single.php file-
$content = stripe_images_from_content(get_the_content());
Now you need to do two things. $content->images contains all the images inserted in the post content area. And $content->content holds the raw content images stripped off. First you need to use $content->images wisely where you want to display your images and second replace the the_content() function with echo wpautop($content->content);
Hope this might help you.

Can't Echo Custom Field In Widget

I'm trying to display a custom field in a widget I'm customizing but I can't get the custom field to display. I think it has something to do with the variable I'm using to get the post ID in the loop because when I change it to the standard the_title function, the widget works, so it has something to do with how I'm calling the custom field.
I know the key to the custom field is "wpcf-promo-title" but no matter what I've tried, the custom field (which holds a shortened version of the post title for the sidebar) just won't display. This code results in the thumbnails showing, but not the promo title. You can see this in action at http://www.cantstopshipping.com
Here's my code, including the query and the front end of the widget.
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Recent Posts' : $instance['title'], $instance, $this->id_base);
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
if ( ! $number = absint( $instance['number'] ) ) $number = 5;
if( ! $cats = $instance["cats"] ) $cats='';
// array to call recent posts.
$crpw_args=array(
'showposts' => $number,
'category__in'=> $cats,
);
$crp_widget = null;
$crp_widget = new WP_Query($crpw_args);
echo $before_widget;
// Widget title
echo $before_title;
echo $instance["title"];
echo $after_title;
// Post list in widget
echo "<ul>\n";
while ( $crp_widget->have_posts() )
{
$crp_widget->the_post();
?>
<li class="crpw-item">
<p style="float:left">
<?php the_post_thumbnail('sidebar-small'); ?>
</p>
<?php $promotitle = get_post_meta($post->ID, 'wpcf-promo-title', true); ?>
<p style="float:right; width:200px">
<?php echo $promotitle; ?>
</p>
<?php if ( $show_date ) : ?>
<span class="crpw-date"><?php echo "("; ?><?php echo get_the_date(); ?><?php echo ")"; ?></span>
<?php endif; ?>
</li>
<?php
}
wp_reset_query();
echo "<div class=\"fix\"></div>";
echo "</ul>\n";
echo $after_widget;
}
It looks like your global $post is missing.
But you could try get_the_ID() instead of $post->ID.
You should also consider getting rid of extract(), it's now considered a "bad" practice.
Another thing is that you should use wp_reset_postdata() to restore the global $post object. The wp_reset_query() call should be used with the query_posts() call.

Query archives by shortcode

What's query for WordPress archives that I can put in functions and call by shortcode...
<?php do_shortcode('[archives]'); ?>
inside archive.php
Found get_queried_object();, I don't know is it right function.
Instead of
<?php while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
/* Custom Archives Functions Go Below this line */
/* Custom Archives Functions Go Above this line */
</div><!-- .entry-content -->
<?php endwhile; // end of the loop. ?>
I want to call it by
<?php do_shortcode('[archives]'); ?>
I want to make function, something like
function archives( $atts, $content = null ) {
if ( have_posts() ) : while ( have_posts() ) : the_post();
$return .= '<div class="archives_posts">';
$return .= '<div class="archives_posts_title">';
$return .= ''.get_the_title($ID).'';
$return .= '</div>';
$return .= '</div>';
endwhile;
endif;
return $return;
}
add_shortcode( 'archives', 'archives' );
Add following codes in functions.php of your theme.
function my_archives($params, $content = null) {
extract(shortcode_atts(array(
'type' => 'style1'
), $params));
ob_start();
?>
<div class="archives_posts">
<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 10 ) ); ?>
</div>
<?php return ob_get_clean();
}
add_shortcode('archives','my_archives');
You can change arguments to list archive posts as per your requirement.
Use following shortcode to get list of archive posts
Use <?php echo do_shortcode('[archives]'); ?> in template
Use [archives] in posts/pages
Add the following lines to your /wp-content/themes/themeName/functions.php:
function sc_archive(){
return wp_get_archives();
}
add_shortcode('archives','sc_archive');
Then use it in your Page: [archives].

Customizing a Wordpress-generated breadcrumb link

Good Day
I am using Wordpress, and I am using a breadcrumbs plugin (Breadcrumb NavXT). Now it works great, exept that I would like to customize the way it links to a specific link:
Now here is the scenario:
I have a page, and on the page are links to posts. So when you click on one of the links on the page, it takes you to the post. Now this posts belongs to a category named Drill Rigs, and Drill Rigs is a child category of Products.
Now on the post page, which has category 'products>drill rigs' (parent/child), the breadcrumb shows the following way:
Home>products>drill rigs>postname
Problem is, that of you click on the products link above, it takes you to the category page of products:
siteurl/category/products
and not the actual wordpress page that is called products (that contains the links linking to the posts mentioned above:
siteurl/absolute link to the page, which is a unique link eg. siteurl/803-2
Now as far as I know, you cannot change the path from a category (which are unique to posts) to a page, neither in wordpress nor the plugin.
The best way I can think of is to add custom jquery or php to the posts php page (or index/header pages) that looks for the container div containing the anchor tags that hosts the title 'products'...and then replacing that anchor tag with my custom unique page url...
How would I do this...?
function the_breadcrumb() {
$sep = '/';
if (!is_front_page()) {
echo '<div class="breadcrumbs">';
echo '<a href="';
echo get_option('home');
echo '">';
bloginfo('name');
echo '</a>' . $sep;
if (is_category() || is_single() ){
the_category('title_li=');
} elseif (is_archive() || is_single()){
if ( is_day() ) {
printf( __( '%s', 'text_domain' ), get_the_date() );
} elseif ( is_month() ) {
printf( __( '%s', 'text_domain' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
} elseif ( is_year() ) {
printf( __( '%s', 'text_domain' ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
} else {
_e( 'Blog Archives', 'text_domain' );
}
}
if (is_single()) {
echo $sep;
the_title();
}
if (is_page()) {
echo the_title();
}
if (is_home()){
global $post;
$page_for_posts_id = get_option('page_for_posts');
if ( $page_for_posts_id ) {
$post = get_page($page_for_posts_id);
setup_postdata($post);
the_title();
rewind_posts();
}
}
echo '</div>';
}
}
try this hope this may help you
This code works with asynchron content for your posts: (to copy in functions.php)
function ariane() {
$cat_id = get_the_category()[0]->term_id;
$breadcrumb = '<li>' . get_the_title() . '</li>';
$if_parent = TRUE;
while($if_parent == TRUE) :
$cat_object = get_category($cat_id);
$cat = $cat_object->term_id;
$categoryURL = get_category_link($cat);
$name = $cat_object->name;
$cat_id = $cat_object->parent;
$add_link = '<li>' . $name . '</li>';
$breadcrumb = substr_replace($breadcrumb, $add_link, 0, 0);
if($cat_id == 0) :
$if_parent = FALSE;
endif;
endwhile;
echo '<ul>' . $breadcrumb . '</ul>';
}
In your archive.php or in the loop WP_QUERY
<div class="ariane"><?php ariane(); ?></div>
in css :
.ariane ul{
display: flex;
justify-content: flex-start;
align-items: center;
}
.ariane li:not(:last-child):after {
content: '>';
margin: 0 5px;
}
you can use the function to set your Breadcrumb by coding :
function the_breadcrumb() {
if (!is_home()) {
echo '<a href="';
echo get_option('home');
echo '">';
echo "Home";//bloginfo('name');
echo "</a> / ";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
echo " / ";
the_title();
}
} elseif (is_page()) {
echo the_title();
}
}
}
Put the above code in function.php file
you have to just call the function where you want to show the bredcrumb
<?php the_breadcrumb(); ?>
Hope this will help you ...
<script type="text/javascript">
$('#breadcrumbs .category[href="http://mysite.com/category/products"]').attr('href','http://mysite.com/803-2');
</script>
where:
http://mysite.com/803-2
is the url of the p[age I want to link to...

Categories